package czx.wt.util.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author JokerJodas
 * @date 2018.6.11
 */
public class ResponseData {

    private static Logger logger = LoggerFactory.getLogger(ResponseData.class);

    /**
     * 仅反馈成功，无详细数据，请用这个
     */
    public static final ResponseData SUCCESS = new ResponseData(CommResponseEnum.SUCCESS);

    /**
     * 未知错误，无详细数据，请用这个
     */
    public static final ResponseData FAILURE = new ResponseData(CommResponseEnum.FAILURE);

    private int code;
    private String msg;
    private Object data;

    /**
     * 异常码反馈
     *
     * @param responseEnum
     */
    public ResponseData(IResponseEnum responseEnum) {
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
    }

    /**
     * 成功消息
     *
     * @param data
     */
    public ResponseData(Object data) {
        this(CommResponseEnum.SUCCESS, data);
    }

    /**
     * 异常时还需要数据的情况
     *
     * @param responseEnum
     * @param data
     */
    public ResponseData(IResponseEnum responseEnum, Object data) {
        this.code = responseEnum.code();
        this.msg = responseEnum.msg();
        this.data = data;
    }

    /**
     * 成功消息
     *
     * @param map
     */
    public static String turnResponse(Object map) throws JsonProcessingException {
        Map<String, Object> resMap = new HashMap<>();
        if (map instanceof Pager) {
            resMap.put("code", CommResponseEnum.SUCCESS.code());
            resMap.put("msg", CommResponseEnum.SUCCESS.msg());
            resMap.put("totalCount", ((Pager) map).getTotalRecord());
            resMap.put("data", ((Pager) map).getDataList());
        } else if (map instanceof Page) {
            resMap.put("code", CommResponseEnum.SUCCESS.code());
            resMap.put("msg", CommResponseEnum.SUCCESS.msg());
            resMap.put("totalCount", ((Page) map).getTotal());
            resMap.put("data", map);
        } else {
            resMap.put("code", CommResponseEnum.SUCCESS.code());
            resMap.put("msg", CommResponseEnum.SUCCESS.msg());
            resMap.put("data", map);
        }
        ObjectMapper mapper = new ObjectMapper();
        //jackson 实体转json 为NULL或者为空不参加序列化
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        logger.info("返回结果：" + mapper.writeValueAsString(resMap));
        return mapper.writeValueAsString(resMap);
    }

    /**
     * 成功消息
     *
     * @param
     */
    public static String OKResponse() throws JsonProcessingException {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("code", CommResponseEnum.SUCCESS.code());
        resMap.put("msg", CommResponseEnum.SUCCESS.msg());
        logger.info("返回结果：" + new ObjectMapper().writeValueAsString(resMap));
        return new ObjectMapper().writeValueAsString(resMap);
    }

    /**
     *@Author:ChenZhiXiang
     *@Description: 失败信息，但事物不回滚
     *@Date: 15:15 2018/8/30
     */
    public static String failureResponse(CommResponseEnum responseEnum) throws Exception {
        Map<String,Object> respMap = new LinkedHashMap<>();
        respMap.put("code",responseEnum.code());
        respMap.put("msg",responseEnum.msg());
        return new ObjectMapper().writeValueAsString(respMap);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}


